<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="../statics/css/bootstrap.min copy.css">
</head>
<style>
    .col-2 {
        padding-top: 7px;
        font-family: fantasy;
        font-size: 20px;
        color: black;
        margin-left: 15px;
    }

    .form-check {
        padding-left: 7rem;
    }

    .form-group {
        margin-left: 5rem;
    }

    .align-middle {
        margin-left: 16rem;
        margin-bottom: 2rem;
    }

    .btn {
        margin-left: 9rem;
        color: black;
        background-color: #fff;
        font-family: fantasy;
        font-size: 30px;
    }

    .form-control {

        border-bottom: black solid 1px;
    }
</style>

<body background="../statics/Photo/Cities.jpg" style="background-repeat: no-repeat;
background-size: 100% 100%;
background-attachment: fixed;">

    <div class="container">
        <div class="row align-items-center">
            <div class="col">

            </div>
            <div class="col">
                <div class="alert alert-danger alert-dismissible fade show invisible text-center"
                    style="width: 42rem;top: 100px;right: -10px;" role="alert">
                    <strong>Tips:</strong><label for="" id="msg">??</label>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            </div>
            <div class="col">

            </div>
        </div>
    </div>
    <div class="row align-items-center" style="height: 50rem;">
        <div class="col">

        </div>
        <div class="col">

        </div>
        <div class="col" style="right: 580px;">
            <div style="width: 48rem;height: 40rem;opacity: 0.8">
                <div class="card-body align-items-center">
                    <br>
                    <br>
                    <h1 class="align-middle" style="font-family: fantasy;font-size: 50px;margin-left: 150px;">Write Life📕</h1>
                    <br>
                    <br>
                    <form id="loginForm">

                        <div class="form-group row">
                            <label class="col-2" for="exampleInputEmail1">User:</label>
                            <input type="text" class="form-control col-6" name="username" id="username"
                                aria-describedby="emailHelp">
                        </div>
                        <br>
                        <br>
                        <br>
                        <div class="form-group row">
                            <label class="col-2" for="exampleInputPassword1">PWD:</label>
                            <input type="password" class="form-control col-6" name="password" id="password">
                        </div>
                        <br>
                        <br>
                        <br>
                        <button type="button" class="btn btn-primary" onclick="login()">Login</button>
                        <button type="button" class="btn btn-primary" onclick="register()">Register</button>
                    </form>
                </div>
            </div>
        </div>
    </div>





    <script src="../statics/js/jquery-3.6.0.min.js"></script>
    <script src="../statics/js/bootstrap.min.js"></script>
    <script>
        function login() {
            let username = $('#username').val();
            let password = $('#password').val();
            if (username.length > 0 && password.length > 0) {
                // ajax请求
                $.ajax({
                    url: "/loginDone",
                    type: "POST",
                    data: $('#loginForm').serialize(),
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                        if (res.code === 200) {
                            window.location.href = "/";
                        } else {
                            alert_fn(res.msg);
                        }

                    },
                    error: function (msg) {
                        alert_fn('Net Incorrect');
                    }
                })
            } else {// 
                alert_fn('User or PWD can”t null');
                return false;
            }
        }

        function alert_fn(msg) {
            msg = msg || '密码不能为空，两次密码应该一致';
            $('#msg').text(msg);
            $('.invisible').removeClass('invisible');
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 5000);
        }

        function register() {
            window.location.href = '/register'
        }

    </script>
</body>

</html>